#include <string.h>
#include "compiler.h"
#include "rtc.h"

uint32_t get_fattime(void);
uint32_t get_fattime(void)
{
	uint32_t ul_time, ul_tmp;
	rtc_string_t _rtc_fat;
	char _str[3], *_end;

	/* Retrieve date and time */
	vReadRTC(&_rtc_fat);
	
	/* Convert read BCD format to FAT time format. */

	/* Formatting the year. */
	_str[0] = _rtc_fat.date2[8];
	_str[1] = _rtc_fat.date2[9];
	_str[2] = '\0';
	ul_tmp = (char)strtoul((const char *)_str, &_end, 10);
	ul_tmp = ul_tmp + 20;
	ul_tmp = ul_tmp << 25;
	ul_time = ul_tmp;
	
	/* Formatting the month. */
	_str[0] = _rtc_fat.date2[3];
	_str[1] = _rtc_fat.date2[4];
	_str[2] = '\0';
	ul_tmp = (char)strtoul((const char *)_str, &_end, 10);
	ul_tmp = ul_tmp << 21;
	ul_time |= ul_tmp;
	
	/* Formatting the day. */
	_str[0] = _rtc_fat.date2[0];
	_str[1] = _rtc_fat.date2[1];
	_str[2] = '\0';
	ul_tmp = (char)strtoul((const char *)_str, &_end, 10);
	ul_tmp = ul_tmp << 16;
	ul_time |= ul_tmp;
	
	/* Formatting the hour. */
	_str[0] = _rtc_fat.time[0];
	_str[1] = _rtc_fat.time[1];
	_str[2] = '\0';
	ul_tmp = (char)strtoul((const char *)_str, &_end, 10);
	ul_tmp = ul_tmp << 11;
	ul_time |= ul_tmp;

	/* Formatting the minute. */
	_str[0] = _rtc_fat.time[3];
	_str[1] = _rtc_fat.time[4];
	_str[2] = '\0';
	ul_tmp = (char)strtoul((const char *)_str, &_end, 10);
	ul_tmp = ul_tmp << 5;
	ul_time |= ul_tmp;

	/* Formatting the second. */
	_str[0] = _rtc_fat.time[6];
	_str[1] = _rtc_fat.time[7];
	_str[2] = '\0';
	ul_tmp = (char)strtoul((const char *)_str, &_end, 10);
	ul_tmp = ul_tmp/2;
	ul_time |= ul_tmp;

	return ul_time;
}

